﻿namespace ProcessManager
{
    internal class ProcessManagerServiceWindows : System.ServiceProcess.ServiceBase
    {
        #region Properties

        private System.Threading.Thread _processManagerThread;
        private ProcessManagerEngine _processManagerEngine;

        #endregion

        #region constructor

        internal ProcessManagerServiceWindows()
        {
            this.ServiceName = this.GetType().ToString();
        }

        #endregion

        #region Methods

        static void Main()
        {
            if (System.Environment.UserInteractive)
            {
                ProcessManagerServiceWindows service = new ProcessManagerServiceWindows();
                service.TestStartupAndStop();
            }
            else
            {
                System.ServiceProcess.ServiceBase[] ServicesToRun;
                ServicesToRun = new System.ServiceProcess.ServiceBase[] { new ProcessManagerServiceWindows() };
                System.ServiceProcess.ServiceBase.Run(ServicesToRun);   
            }
        }

        internal void TestStartupAndStop()
        {
            this.OnStart(null);
            System.Console.ReadLine();
            this.OnStop();
            System.Console.ReadLine();
        }

        protected override void OnStart(string[] args)
        {
            System.Console.WriteLine("On Start");
            base.OnStart(args);

            this._processManagerEngine = new ProcessManagerEngine();
            this._processManagerThread = new System.Threading.Thread(new System.Threading.ThreadStart(this._processManagerEngine.Run));
            this._processManagerThread.Start();
        }

        protected override void OnStop()
        {
            this._processManagerThread.Abort();

            base.OnStop();
            System.Console.WriteLine("On Stop");
        }

        #endregion
    }
}